package java.util.stream.deadLock;

/**
 * @author fangjiaxiaobai@gmail.com
 * @date 2020-05-18
 * @since 1.0.0
 */
public class Demo {

    public static void main(String[] args) {
        new Thread(new HoldLockThread("lockA","lockB")).start();
        new Thread(new HoldLockThread("lockB","lockA")).start();
    }

}


class HoldLockThread implements Runnable {

    private final String lockA;

    private final String lockB;

    public HoldLockThread(String lockA, String lockB) {
        this.lockA = lockA;
        this.lockB = lockB;
    }

    @Override
    public void run() {

        synchronized (lockA) {
            System.out.println("持有 " + lockA + ",还想获取 " + lockB);
            try {
                Thread.sleep(2000L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            synchronized (lockB) {
                System.out.println("持有 " + lockB + ",还想获取 " + lockA);
            }
        }

    }
}